package interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author：汤小洋
 * Date：2022-11-28 11:07
 * Description：<描述>
 */
public class HelloInterceptor implements HandlerInterceptor {

    // 在调用目标处理方法之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("HelloInterceptor.preHandle............");

        /**
         * 用户登陆的权限拦截
         */
        Object username = request.getSession().getAttribute("username");
        if(username == null){
            response.sendRedirect(request.getContextPath()+"/showLogin");
            return false;
        }
        return true; // true表示放行，继续调用目标处理方法，false表示不放行
    }

    // 调用目标处理方法之后执行，渲染视图之前
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("HelloInterceptor.postHandle");
    }

    // 渲染视图之后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("HelloInterceptor.afterCompletion");
    }
}
